
<template>

  <!-- 添加或修改对话框 -->
  <el-dialog :title="title" :visible.sync="deliverOpen" :width="dialogWidth" :close-on-click-modal="false" @close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">

      <el-form-item label="人才信息" prop="talentId">
        <el-input
          v-model="form.talentId"
          type="hidden"
          style="display: none"
        />
        <el-autocomplete
          v-model="formNameOrPhone"
          :fetch-suggestions="queryTalent"
          placeholder="请输入姓名或者电话"
          :maxlength="25"
          @select="selectTalent4Form"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="company">
        <el-input
          v-model="form.company"
          placeholder="公司名称"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="department">
        <el-input
          v-model="form.department"
          placeholder="部门名称"
        />
      </el-form-item>
      <el-form-item label="薪酬" prop="salary">
        <el-input
          v-model="form.salary"
          placeholder="薪酬"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="joinTime">
        <el-date-picker
          v-model="form.joinTime"
          type="datetime"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="备注信息" prop="note">
        <el-input
          v-model="form.note"
          placeholder="备注信息"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 11}"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listHtTalent, getHtTalent } from '@/api/hunter/ht-talent'
import { addHtDeliver, updateHtDeliver } from '@/api/hunter/ht-deliver'

export default {
  name: 'HtDeliver',
  components: {
  },
  props: {
    deliverForm: {
      type: Object,
      default: null
    },
    deliverOpen: {
      type: Boolean,
      default: false
    },
    projectId: {
      type: Number,
      default: null
    },
    onClose: {
      type: Function,
      required: true
    },
    deliverTalentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: '',
      isEdit: false,

      formNameOrPhone: '',

      // 表单参数
      form: {
      },
      // 表单校验
      rules: { talentId: [{ required: true, message: '人才不能为空' }],
        company: [{ required: true, message: '公司名称不能为空', trigger: 'blur' }],
        department: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
        salary: [{ required: true, message: '薪酬不能为空', trigger: 'blur' }],
        joinTime: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }]
      }
    }
  },

  computed: {
    dialogWidth() {
      return window.innerWidth <= 600 ? '90%' : '50%'
    }
  },
  created() {
    if (this.deliverForm != null && this.deliverForm.id != null) {
      this.title = '修改交付'
      this.isEdit = true
      this.form = this.deliverForm
      this.formNameOrPhone = this.form.talent.name + '【' + this.form.talent.phone + '】'
    } else {
      this.title = '添加交付'
      this.isEdit = false
      if (this.deliverTalentId != null) {
        this.form.talentId = this.deliverTalentId
        getHtTalent(this.deliverTalentId).then(res => {
          this.formNameOrPhone = res.data.name + '【' + res.data.phone + '】'
        })
      }
    }
  },

  methods: {
    selectTalent4Form(item) {
      this.formNameOrPhone = item.value
      this.form.talentId = item.talent.id
    },
    queryTalent(queryString, cb) {
      if (queryString) {
        listHtTalent({
          nameOrPhone: queryString
        }).then(res => {
          var talentList = res.data
          if (talentList.list.length > 0) {
            var temp = []
            talentList.list.forEach(item => {
              temp.push({ value: item.name + '【' + item.phone + '】', talent: item })
            })
            cb(temp)
          }
        })
      }
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.onClose()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        talentId: undefined,
        company: undefined,
        department: undefined,
        salary: undefined,
        joinTime: undefined,
        note: undefined
      }
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateHtDeliver(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(response.msg)
                this.onClose()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addHtDeliver(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(response.msg)
                this.onClose()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    }
  }
}
</script>
